package com.zzyk.microCloud.controller;


import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.aspectj.annotation.OperaLog;
import com.zzyk.main.model.vo.LogInfo;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/sys/log")
public class SysLogController {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * 列出可快速查询的操作关键字 (opera字段)
     *
     * @return 可快速查询的操作关键字
     */
    @RequestMapping("listOperas")
    @ResponseBody
    public Message listLogOperas() {
        Message message = Message.success();
        message.addData("data", sysLogService.getOperas());
        return message;
    }

    /**
     * 获取操作日志 分页
     *
     * @param operation   操作类型(快捷查询的类目)
     * @param searchValue 查询关键字
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param currentPage 当前页 默认1
     * @param pageSize    分页大小 默认10
     * @return 符合上列条件的操作日志
     */
    @RequestMapping("list")
    @ResponseBody
    public Message listLog(
            @RequestParam(value = "operation", required = false) String operation,
            @RequestParam(value = "searchValue", required = false) String searchValue,
            @RequestParam(value = "startTime", required = false) Date startTime,
            @RequestParam(value = "endTime", required = false) Date endTime,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Message message = Message.success();
        PageInfo<LogInfo> logList = sysLogService.getLogList(operation, searchValue, startTime, endTime, currentPage, pageSize, null);
        message.addData("data", logList.getList());
        message.addData("total", logList.getTotal());
        message.addData("pages", logList.getPages());
        message.addData("pageNum", logList.getPageNum());
        return message;
    }

    @OperaLog("导出系统日志")
    @RequestMapping("export")
    @ResponseBody
    public Message exportLog(
            @RequestParam(value = "operation", required = false) String operation,
            @RequestParam(value = "searchValue", required = false) String searchValue,
            @RequestParam(value = "startTime", required = false) Date startTime,
            @RequestParam(value = "endTime", required = false) Date endTime) {
        Message message = Message.success();
        message.addData("data", sysLogService.exportLogList(operation, searchValue, startTime, endTime));
        return message;
    }
}
